<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="800px"
    @close="close"
  >
    <el-form ref="formRef" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="公司" prop="CompanyID">
        <el-cascader
          v-model="form.CompanyID"
          style="width: 100%"
          :options="CompanyOptions"
          :props="companyProps"
          clearable
          @change="getDeptment"
        />
      </el-form-item>
      <el-form-item label="部门" prop="DepartmentID">
        <el-cascader
          v-model="form.DepartmentID"
          style="width: 100%"
          :options="DeptmentOptions"
          :props="companyProps"
          clearable
          @change="getStaffPost"
        />
      </el-form-item>
      <el-form-item label="员工" prop="StaffID">
        <el-cascader
          v-model="form.StaffID"
          style="width: 100%"
          :options="staffList"
          :props="StaffProps"
          clearable
          @change="selectEmployees"
        />
      </el-form-item>
      <el-form-item label="消息标题" prop="">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="消息内容" prop="">
        <el-input v-model="form.title" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">发 送</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import {
    getStaffPostRoleSelect,
    getBusinessClassSelect,
    getCompanySelect,
    getDeptmentSelect,
    GetStaffSelect,
  } from '@/api/common'
  import { doEdit } from '@/api/messageCenter'

  export default defineComponent({
    name: 'MessageCenterEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        companyProps: {
          multiple: true,
        },
        StaffProps: {
          label: 'StaffName',
          value: 'StaffID',
          multiple: true,
        },
        DeptmentOptions: [],
        CompanyOptions: [],
        staffList: [],
        BusinessClassOptions: [],
        StaffPostRoleOptions: [],
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          title: [{ required: true, trigger: 'blur', message: '请输入标题' }],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '发消息'
        } else {
          state.title = '发消息'
          state.form = Object.assign({}, row)
        }
        getOptions()
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {}
        state.staffList = []
        state.DeptmentOptions = []
        state.CompanyOptions = []

        state.dialogFormVisible = false
      }

      // 选择组织后
      const getDeptment = async () => {
        console.log('form.Company', state.form.CompanyID)
        if (state.form.CompanyID.length != 0) {
          const getParams = {
            Company: state.form.CompanyID,
          }
          const {
            data: { DeptmentOptions },
          } = await getDeptmentSelect(getParams)
          state.DeptmentOptions = DeptmentOptions
        } else {
          state.form.DepartmentID = []
          state.DeptmentOptions = []
          state.form.StaffID = []
          state.staffList = []
        }
      }

      // 选择部门后
      const getStaffPost = async () => {
        console.log(state.form.DepartmentID, 'state.form.TaskDeptment')
        if (state.form.DepartmentID.length != 0) {
          const getParams = {
            Deptment: state.form.DepartmentID,
            StaffPostID: [],
          }
          console.log(getParams, 'getParamsgetParamsgetParams')
          const {
            data: { StaffPostOptions },
          } = await GetStaffSelect(getParams)
          state.staffList = StaffPostOptions
        } else {
          state.form.StaffID = []
          state.staffList = []
        }
      }
      // 选择员工后
      const selectEmployees = async () => {
        const getParams = {
          Company: state.form.CompanyID,
          Deptment: state.form.DepartmentID,
          StaffPostID: state.form.StaffPostID,
        }
        console.log(getParams, '123123')
      }

      const getOptions = async () => {
        state.listLoading = true
        const {
          data: { BusinessClassOptions },
        } = await getBusinessClassSelect()
        const {
          data: { CompanyOptions },
        } = await getCompanySelect()
        const {
          data: { StaffPostRoleOptions },
        } = await getStaffPostRoleSelect()
        state.BusinessClassOptions = BusinessClassOptions
        state.CompanyOptions = CompanyOptions
        state.StaffPostRoleOptions = StaffPostRoleOptions
        state.listLoading = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        getOptions,
        getStaffPost,
        getDeptment,
        selectEmployees,
        close,
        save,
      }
    },
  })
</script>
